﻿<cfscript>
/** 
* @hint "acadmicBase - Term"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.termListener" 
	output=false 
	depends="termPropertyAdvice, sequenceTerm" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	

	/**
	* @hint "根据学期代码读取学期信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermByID( required MachII.framework.Event event ) output=false {

		var termId = event.getArg("TID");

		if ( len(termId) eq 3 ) {

			var sql = "SELECT 
						term_id, academic_year, academic_term, term_start, term_weeks, year_term  
						FROM t_term 
						WHERE term_id = :termId ";

			var queryObj = new Query( datasource=application.dnsSlave );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );

			var rs = queryObj.execute( sql=sql ).getResult();

			if ( rs.recordCount eq 1 ) {
				event.setArg("TermID", rs.term_id );
				event.setArg("AcademicYear", rs.academic_year );
				event.setArg("AcademicTerm", rs.academic_term );
				event.setArg("DateStart", rs.term_start );
				event.setArg("Weeks", rs.term_weeks );
				event.setArg("YearTerm", rs.year_term );
			}

		}

	}

	/**
	* @hint "读取当前学期"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermRunning( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 1, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermID", rs.term_id );
			event.setArg("AcademicYear", rs.academic_year );
			event.setArg("AcademicTerm", rs.academic_term );
			event.setArg("DateStart", rs.term_start );
			event.setArg("Weeks", rs.term_weeks );
		}

	}
	
	/**
	* @hint "读取费用结算学期"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermCharge( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 7, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermID", rs.term_id );
			event.setArg("AcademicYear", rs.academic_year );
			event.setArg("AcademicTerm", rs.academic_term );
			event.setArg("DateStart", rs.term_start );
			event.setArg("Weeks", rs.term_weeks );
		}

	}
	
	/**
	* @hint "读取当前排课学期"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermForCourse( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 2, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermID", rs.term_id );
			event.setArg("AcademicYear", rs.academic_year );
			event.setArg("AcademicTerm", rs.academic_term );
			event.setArg("DateStart", rs.term_start );
			event.setArg("Weeks", rs.term_weeks );
		}
		
	}
	
	/**
	* @hint "读取当前排课学期, 标注为PK"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermForCoursePK( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 2, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermIDPK", rs.term_id );
			event.setArg("AcademicYearPK", rs.academic_year );
			event.setArg("AcademicTermPK", rs.academic_term );
			event.setArg("DateStartPK", rs.term_start );
			event.setArg("WeeksPK", rs.term_weeks );
		}
		
	}
	
	/**
	* @hint "读取当前排考学期"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermForExam( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 3, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermID", rs.term_id );
			event.setArg("AcademicYear", rs.academic_year );
			event.setArg("AcademicTerm", rs.academic_term );
			event.setArg("DateStart", rs.term_start );
			event.setArg("Weeks", rs.term_weeks );
		}
		
	}
	
	/**
	* @hint "读取当前补考学期, 标注为RST"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermForResitExamRST( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 6, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermIDRST", rs.term_id );
			event.setArg("AcademicYearRST", rs.academic_year );
			event.setArg("AcademicTermRST", rs.academic_term );
			event.setArg("DateStartRST", rs.term_start );
			event.setArg("WeeksRST", rs.term_weeks );
		}
		
	}
	
	/**
	* @hint "读取当前补考学期"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermForResitExam( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 6, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermID", rs.term_id );
			event.setArg("AcademicYear", rs.academic_year );
			event.setArg("AcademicTerm", rs.academic_term );
			event.setArg("DateStart", rs.term_start );
			event.setArg("Weeks", rs.term_weeks );
		}
		
	}
	
	/**
	* @hint "读取当前选课学期"
	* 
	* @event "MachII Event Object"
	*/
	public void function getTermForChoose( required MachII.framework.Event event ) output=false {
		
		var sql = "SELECT 
					term_id, academic_year, academic_term, term_start, term_weeks
					FROM t_term 
					WHERE substr(term_prop, 5, 1) = '1' ";
		
		var queryObj = new Query( datasource=application.dnsSlave );

		var rs = queryObj.execute( sql=sql ).getResult();
		
		if ( rs.recordCount eq 1 ) {
			event.setArg("TermID", rs.term_id );
			event.setArg("AcademicYear", rs.academic_year );
			event.setArg("AcademicTerm", rs.academic_term );
			event.setArg("DateStart", rs.term_start );
			event.setArg("Weeks", rs.term_weeks );
		}
		
	}
	
	/**
	* @hint "更新学期"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateTerm( required MachII.framework.Event event ) output=false {
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			sql = "	UPDATE
						t_term t
					SET
						t.term_start = :dateStart ,
						t.term_weeks = :weeks 
					WHERE
						t.term_id = :termId ";

			queryObj = new Query( datasource = application.dnsMaster ); 
			queryObj.addParam( name = "dateStart", value = replace(event.getArg("DateStart"), "-", "", "ALL"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name = "weeks", value = event.getArg("Weeks"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name = "termId", value = event.getArg("TermID"), cfsqltype="cf_sql_char" );
			queryObj.execute( sql = sql );

			event.setArg("TID", event.getArg("TermID"));

		}
		
	}
	
	/**
	* @hint "根据培养方案 刷新学期数据"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushTerm( required MachII.framework.Event event ) output=false {

		var termEntity = "";
		var termExists = "";
		var termPlanedArray = "";
		var termPlaned = "";
		var id = "";
		var sequenceTerm = getsequenceTerm();
		var propAdvice = gettermPropertyAdvice().init();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
		
			var sql = "	SELECT
							s.academic_year,s.academic_term
						FROM
							t_schema s
						GROUP BY
							s.academic_year,s.academic_term
						ORDER BY
							s.academic_year,s.academic_term";
			
			var queryObj = new Query(datasource = application.dnsMaster);
			var rs_termPlaned = queryObj.execute(sql = sql).getResult();
			
			for(a = 1; a lte rs_termPlaned.recordCount; a++){

				sequenceTerm.setAcademicYear(rs_termPlaned['academic_year'][a]);
				sequenceTerm.setAcademicTerm(rs_termPlaned['academic_term'][a]);
				var id = sequenceTerm.getID();
				
				sql = "	SELECT
							t.term_id
						FROM
							t_term t
						WHERE
							t.term_id = :id ";
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "id", value = id, cfsqltype = "cf_sql_char");
				var rs_termExists = queryObj.execute(sql = sql).getResult();
				

				if( rs_termExists.recordCount eq 0 ){

					/*
					学期属性设置
					*/
					propAdvice.setRunning(false);
					propAdvice.setForCourse(false);
					propAdvice.setForExam(false);
					propAdvice.setForHalfExam(false);
					propAdvice.setForChoose(false);
					
					var dateStart = "";
					var weeks = "";
					
					switch(rs_termPlaned['academic_term'][a]){
						
						case "1":
							/* 秋季学期 */
							dateStart = listFirst(rs_termPlaned['academic_year'][a], "-") & "0901";
							weeks = 20;
							break;
						
						case "2":
							/* 春季学期 */
							dateStart = listLast(rs_termPlaned['academic_year'][a], "-") & "0301";
							weeks = 20;
							break;
						
						case "3":
							/* 夏季小学期 */
							dateStart = listLast(rs_termPlaned['academic_year'][a], "-") & "0701";
							weeks = 6;
							break;
					
					}
					
					sql = "	INSERT INTO
								t_term(term_id,academic_year,academic_term,term_start,term_weeks,term_prop,year_term)
							VALUES(
								:termId ,
								:academicYear ,
								:academicTerm ,
								:termStart ,
								:weeks ,
								:termProp ,
								:yearTerm 
							)";
					
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "termId", value = id, cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "academicYear", value = rs_termPlaned['academic_year'][a], cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "academicTerm", value = rs_termPlaned['academic_term'][a], cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "termStart", value = dateStart, cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "weeks", value = weeks, cfsqltype = "cf_sql_integer");
					queryObj.addParam(name = "termProp", value = propAdvice.stringProperty(), cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "yearTerm", value = "20" & id, cfsqltype = "cf_sql_char");
					queryObj.execute(sql = sql);
					
				}
				
			}
			
		}
		
		
	}
	
}

</cfscript>